# frozen_string_literal: true

module Grape
  module DSL
    module Status
      include Grape::DSL::Settings

      def status(code, *params, &block)
        if code.is_a?(Array)
          codes = code
          codes.each { |code| status(code, *params, &block) } and return
        end

        unless (0..2).include?(params.length)
          raise ArgumentError, "wrong number of arguments (given #{params.length + 1}, expected 2..3)"
        end

        if params[0].is_a?(String)
          message = params[0]
          entity = block_given? ? Class.new(Grape::Entity, &block) : params[1]
        else
          entity = block_given? ? Class.new(Grape::Entity, &block) : params[0]
        end

        options = route_setting(:status) || {}
        options[code] = { message: message, entity: entity }

        namespace_setting :status, options
        route_setting :status, options
      end
    end
  end
end
